﻿using System.Collections.Generic;

namespace KBEngineTool.Core
{
    public class Log : Singleton<Log>
    {
        enum LogType
        {
            D, W, E
        }

        HashSet<ILog> m_logDevices = new HashSet<ILog>();

        public static void Debug(string format, params object[] args)
        {
            Log_Msg(LogType.D, format, args);
        }

        public static void Warning(string format, params object[] args)
        {
            Log_Msg(LogType.W, format, args);
        }

        public static void Error(string format, params object[] args)
        {
            Log_Msg(LogType.E, format, args);
        }

        static void Log_Msg(LogType type, string format, params object[] args)
        {
            switch (type)
            {
                case LogType.D:
                    foreach (var device in Inst.LogDevices)
                        device.Debug(format, args);
                    break;
                case LogType.W:
                    foreach (var device in Inst.LogDevices)
                        device.Warning(format, args);
                    break;
                case LogType.E:
                    foreach (var device in Inst.LogDevices)
                        device.Error(format, args);
                    break;
                default:
                    break;
            }
        }

        protected HashSet<ILog> LogDevices
        {
            get
            {
                return m_logDevices;
            }
        }

        public bool AddLogDevices(ILog logDevices)
        {
            return m_logDevices.Add(logDevices);
        }

        public bool RemoveDevices(ILog logDevices)
        {
            return m_logDevices.Remove(logDevices);
        }

        protected override void Init()
        {
            KBEngine.Logger.LogProxy += Logger_LogProxy;
        }

        private void Logger_LogProxy(KBEngine.Logger.LogType arg1, object arg2)
        {
            if (arg1 == KBEngine.Logger.LogType.D)
                Log_Msg(LogType.D, arg2.ToString());
            else if (arg1 == KBEngine.Logger.LogType.W)
                Log_Msg(LogType.W, arg2.ToString());
            else if (arg1 == KBEngine.Logger.LogType.E)
                Log_Msg(LogType.E, arg2.ToString());
        }

        protected override void DisposeUnmanaged()
        {
            KBEngine.Logger.LogProxy -= Logger_LogProxy;
            base.DisposeUnmanaged();
            foreach (var item in m_logDevices)
                item.Dispose();
        }
    }
}
